"""======== 1. 序列化器,序列化单个书籍对象 ======== """""
from booktest.serializers import BookInfoSerializer

from booktest.models import BookInfo

# 1.获取书籍
book = BookInfo.objects.get(id=1)

# 2.创建序列化器对象
serializer = BookInfoSerializer(instance=book)

# 3.序列化
serializer.data

"""
结果:{'id': 1, 'is_delete': False, 'btitle': '射雕英雄传', 'bcomment': 34, 'bpub_date': '1980-05-01', 'bread': 12}

"""

"""======== 2. 序列化器,序列化多个书籍对象(列表) ======== """""
from booktest.serializers import BookInfoSerializer
from booktest.models import BookInfo

# 1.获取书籍
book = BookInfo.objects.all()

# 2.创建序列化器对象,many=True, 传入的多个对象(列表)
serializer = BookInfoSerializer(instance=book, many=True)

# 3.序列化
serializer.data

"""
结果:[OrderedDict([('id', 1), ('btitle', '射雕英雄传'), ('bpub_date', '1980-05-01'), ('bread', 12), ('bcomment', 34), ('is_deletalse)]),
OrderedDict([('id', 2), ('btitle', '天龙八部'), ('bpub_date', '1986-07-24'), ('bread', 36), ('bcomment', 40), ('is_delete'lse)]),
OrderedDict([('id', 3), ('btitle', '笑傲江湖'), ('bpub_date', '1995-12-24'), ('bread', 20), ('bcomment', 80), ('is_delete',se)]),
OrderedDict([('id', 4), ('btitle', '雪山飞狐'), ('bpub_date', '1987-11-11'), ('bread', 58), ('bcomment', 24), ('is_delete', e)])]

"""

"""======== 3. 英雄序列化器,关联外键(掌握) ======== """""
from booktest.serializers import HeroInfoSerializer
from booktest.models import HeroInfo

# 1.获取单个英雄
hero = HeroInfo.objects.get(id=1)

# 2.创建序列化器对象
serializer = HeroInfoSerializer(instance=hero)

# 3.输出结果
serializer.data

"""
结果:{'hname': '郭靖', 'hcomment': '降龙十八掌', 'hgender': 1, 'id': 1}

"""

"""======== 4. 反序列化,数据类型验证 ======== """""
from booktest.serializers import BookInfoSerializer

# 1.准备数据
book_dict = {
    "id": 10,
    "btitle": "金瓶x",
    "bpub_date": "1990-1-1",
    "bread": "哈哈"
}
# 2.创建序列化器
serializer = BookInfoSerializer(data=book_dict)

# 3.校验
serializer.is_valid(raise_exception=True)

"""======== 5.反序列化,选项验证======== """""
from booktest.serializers import BookInfoSerializer

# 1.准备数据
book_dict = {
    "id": 10,
    "btitle": "金xx",
    "bpub_date": "1999-1-1",
}

# 2.创建序列化器
serializer = BookInfoSerializer(data=book_dict)

# 3.校验
serializer.is_valid(raise_exception=True)

"""======== 6, 反序列化,多个字段validate验证 ======== """
from booktest.serializers import BookInfoSerializer

# 1.准备数据
book_dict = {
    "id": 10,
    "btitle": "金瓶xx",
    "bpub_date": "1999-1-1",
    "bread": 50,
    "bcomment": 8
}

# 2.创建序列化器
serializer = BookInfoSerializer(data=book_dict)

# 3.校验
serializer.is_valid(raise_exception=True)

"""======== 7.反序列化,自定义验证方法(了解) ======== """
from booktest.serializers import BookInfoSerializer

# 1.准备数据
book_dict = {
    "id": 10,
    "btitle": "金瓶xx",
    "bpub_date": "1999-1-1",
    "bread": 50,
    "bcomment": 8
}

# 2.创建序列化器
serializer = BookInfoSerializer(data=book_dict)

# 3.校验
serializer.is_valid(raise_exception=True)

"""======== 8.反序列化,create保存数据 ======== """
from booktest.serializers import BookInfoSerializer

# 1.准备数据
book_dict = {
    "id": 10,
    "btitle": "金瓶xx",
    "bpub_date": "2016-1-1",
    "bread": 50,
    "bcomment": 8
}

# 2.创建序列化器
serializer = BookInfoSerializer(data=book_dict)

# 3.校验
serializer.is_valid(raise_exception=True)

# 4.入库
serializer.save()

"""======== 9.反序列化,update更新数据 ======== """
from booktest.serializers import BookInfoSerializer
from booktest.models import BookInfo

# 1.准备数据
book_dict = {
    "id": 10,
    "btitle": "金瓶xx2",
    "bpub_date": "2016-1-1",
    "bread": 50,
    "bcomment": 8
}
book = BookInfo.objects.get(btitle="金瓶xx")


# 2.创建序列化器
serializer = BookInfoSerializer(instance=book, data=book_dict)

# 3.校验
serializer.is_valid(raise_exception=True)

# 4.入库
serializer.save()

"""======== 10.测试模型类,序列化器,序列化 ======== """
from booktest.serializers import BookInfoModelSerializer
from booktest.models import BookInfo

# 1.获取书籍
book = BookInfo.objects.get(id=1)

# 2.创建序列化器对象
serializer = BookInfoModelSerializer(instance=book)

# 3.序列化
serializer.data
